package com.algorithm;

import java.util.*;

/**
 * 字母异位词分组
 */
public class GroupAnagrams {

    public static List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) {
            return Collections.emptyList();
        }
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (final String str : strs) {
            int[] ints = new int[26];
            Arrays.fill(ints, 0);
            byte[] bytes = str.getBytes();
            for (byte aByte : bytes) {
                ints[aByte - 97]++;
            }
            String s = Arrays.toString(ints);
            if (map.get(s)==null){
                map.put(s,new ArrayList<String>(){{
                    add(str);
                }});
            }else {
                map.get(s).add(str);
            }
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
//       String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
       String[] strs = {"ddddddddddg","dgggggggggg"};
        List<List<String>> lists = groupAnagrams(strs);
        System.out.println(lists);
    }
}
